package com.wtwd.device.service.impl;


import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wtwd.device.dao.AppDeviceStepMapper;
import com.wtwd.device.model.entity.DeviceStep;
import com.wtwd.device.model.entity.DeviceStepDto;
import com.wtwd.device.model.entity.DeviceStepInfo;
import com.wtwd.device.model.vo.DeviceStepVo;
import com.wtwd.device.service.IDeviceStepInfoService;
import com.wtwd.device.service.IDeviceStepService;
import com.wtwd.device.util.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 每日步数 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2024-05-25
 */
@Service
public class DeviceStepServiceImpl extends ServiceImpl<AppDeviceStepMapper, DeviceStep> implements IDeviceStepService {

    @Resource
    private AppDeviceStepMapper deviceStepMapper;

    @Resource
    private IDeviceStepInfoService deviceStepInfoService;


    @Override
    public boolean insertOrUpdate(DeviceStep step) {
        if (step == null) {
            return false;
        }
        return deviceStepMapper.insertOrUpdate(step) > 0 ? true : false;
    }

    @Override
    public DeviceStep getDayStepByTime(Long deviceId, String time) {
        return new LambdaQueryChainWrapper<>(deviceStepMapper)
                .eq(DeviceStep::getDeviceId, deviceId)
                .eq(DeviceStep::getTime, time)
                .one();
    }

    @Override
    public List<DeviceStep> getDaysStepInfo(Long deviceId, String startTime, String endTime) {
        return new LambdaQueryChainWrapper<>(deviceStepMapper)
                .eq(DeviceStep::getDeviceId, deviceId)
                .between(DeviceStep::getTime, startTime, endTime)
                .list();

    }

    @Override
    public DeviceStepVo getDayStepDataByTime(Long deviceId, Long id, String time) {
        DeviceStep deviceStep = getDayStepByTime(deviceId, time);
        if (deviceStep == null) {
            return null;
        }
        DeviceStepVo deviceStepVo = new DeviceStepVo();
        List<DeviceStepInfo> deviceStepInfos = deviceStepInfoService.getDayStepInfoByTotalIdGtId(deviceStep.getId(), id);
        BeanUtils.copyProperties(deviceStep, deviceStepVo);
        deviceStepVo.setStepInfos(deviceStepInfos);
        return deviceStepVo;
    }

    @Override
    public List<DeviceStep> synchronizationStep(List<DeviceStepDto> deviceStepDtos) {
        List<DeviceStep> deviceSteps = new ArrayList<>();
        for (DeviceStepDto deviceStepDto : deviceStepDtos) {
            DeviceStep deviceStep = new DeviceStep();
            BeanUtils.copyProperties(deviceStepDto, deviceStep);
            DeviceStep dayStepByTime = getDayStepByTime(deviceStepDto.getDeviceId(), deviceStepDto.getTime());
            if (dayStepByTime == null) {
                save(deviceStep);
            } else {
                deviceStep.setId(dayStepByTime.getId());
            }
            List<DeviceStepInfo> deviceStepInfos = deviceStepDto.getStepInfos();
            if (deviceStepInfos != null && deviceStepInfos.size() > 0) {
                deviceStepInfos.stream().forEach(e -> {
                    e.setDeviceStepId(deviceStep.getId());
                });
                deviceStepInfoService.saveBatch(deviceStepInfos);
                DeviceStepInfo deviceStepInfo = deviceStepInfoService.getLastDeviceStepInfoByTotalId(deviceStep.getId());
                deviceStep.setLastInfoId(deviceStepInfo.getId());
                updateById(deviceStep);
            }
            deviceSteps.add(deviceStep);
        }
        return deviceSteps;
    }

    @Override
    public boolean bathInsertOrUpdate(List<DeviceStep> deviceSteps) {
        return deviceStepMapper.bathInsertOrUpdate(deviceSteps) > 0 ? true : false;
    }
}
